package com.aaa.service.proxy;

import com.aaa.service.Service1;
import com.aaa.service.impl.Service1Impl;
import com.sun.org.apache.bcel.internal.generic.NEW;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkProxyFactory {
    public Object createProxyObject(Object obj) {
        Object object = Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("开启事务");
                Object o = method.invoke(obj, args);
                System.out.println("提交事务");
                return o;
            }
        });
        return object;
    }
}

class TestJdk {
    public static void main(String[] args) {
        Service1 service1 = new Service1Impl();
        Service1 proxyObject = (Service1) new JdkProxyFactory().createProxyObject(service1);
        proxyObject.myService();
    }
}
